var keyName = "shopCar";

function addShopCar(product){
	var productData = getCar();
	if(!productData){
		var proData = [
			product
		]
		addCar(proData);
	}else{
		var carData = JSON.parse(productData);
		var bool = true;
		for (var i = 0;i<carData.length;i++) {
			if (carData[i].id == product.id) {
				carData[i].num = parseInt(carData[i].num)+parseInt(product.num);
				carData[i].totalPrice = (parseFloat(carData[i].totalPrice) + parseFloat(product.totalPrice)).toFixed(2);
				bool = false;
				break;
			} 
		}
		if(bool){
			carData.push(product);
		}
		addCar(carData);
	}
}

function getCar(){
	return localStorage.getItem(keyName);
}

function addCar(productData){
	localStorage.setItem(keyName,JSON.stringify(productData));
}

function delProduct(id){
	var carData = JSON.parse(getCar());
	var arrData = [];
	for (var i = 0; i < carData.length; i++) {
		if (carData[i].id == id) {
			continue;
		}else{
			arrData.push(carData[i]);
		}
	}
	addCar(arrData);
}

